package com.zjjt.dc.modules.datacatch.job;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zjjt.dc.modules.datacatch.entity.QueryCondition;
import com.zjjt.dc.modules.datacatch.service.IDataCatchService;
import com.zjjt.dc.modules.datacatch.service.IDataExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Slf4j
@Component
public class DataCatchQuartzJob {

    @Autowired
    private IDataCatchService dataCatchService;

    @Autowired
    private IDataExportService dataExportService;


    /**
     * 辽文抓取数据的任务 - 仓库
     *
     * @param condition
     * @throws Exception
     */
    @XxlJob("DataCatch-liao-wen-wh")
    public JSONArray dataCatchQuartzJobForLWWareHouse(@RequestBody QueryCondition condition) throws Exception {
        if (condition == null) {
            condition = new QueryCondition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            Calendar start = Calendar.getInstance();
            start.add(Calendar.MONTH, -3);
            condition.setBeginTime(dateFormat.format(start.getTime()));
            condition.setEndTime(dateFormat.format(now.getTime()));
        }
        int flagUpdateWareHouse = dataCatchService.updateLWWareHouseDatas(condition);

        if (flagUpdateWareHouse < 0) {
            return new JSONArray();
        } else {
            return dataExportService.getExportDataForLW(condition);
        }
    }

    /**
     * 辽文抓取数据的任务 - 客户
     *
     * @param condition
     * @throws Exception
     */
    @XxlJob("DataCatch-liao-wen-customer")
    public JSONArray dataCatchQuartzJobForLWCustomer(@RequestBody QueryCondition condition) throws Exception {
        if (condition == null) {
            condition = new QueryCondition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            Calendar start = Calendar.getInstance();
            start.add(Calendar.MONTH, -3);
            condition.setBeginTime(dateFormat.format(start.getTime()));
            condition.setEndTime(dateFormat.format(now.getTime()));
        }
        int flagUpdateCustomer = dataCatchService.updateLWCustomerDatas(condition);

        if (flagUpdateCustomer < 0) {
            return new JSONArray();
        } else {
            return dataExportService.getExportDataForLW(condition);
        }
    }


    /**
     * 辽文抓取数据的任务 - 持仓
     *
     * @param condition
     * @throws Exception
     */
    @XxlJob("DataCatch-liao-wen-customer-holdings")
    public JSONArray dataCatchQuartzJobForLWCustomerHoldings(@RequestBody QueryCondition condition) throws Exception {
        if (condition == null) {
            condition = new QueryCondition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            Calendar start = Calendar.getInstance();
            start.add(Calendar.MONTH, -3);
            condition.setBeginTime(dateFormat.format(start.getTime()));
            condition.setEndTime(dateFormat.format(now.getTime()));
        }
        int flagUpdateCustomerHoldings = dataCatchService.updateLWCustomerHoldings(condition);

        if (flagUpdateCustomerHoldings < 0) {
            return new JSONArray();
        } else {
            return dataExportService.getExportDataForLW(condition);
        }
    }


    /**
     * 百礼汇抓取数据的任务
     *
     * @param param
     * @throws Exception
     */
    @XxlJob("DataCatch-bai-li-hui")
    public JSONArray dataCatchQuartzJobForBLH(String param) throws Exception {
        QueryCondition condition = null;
        String jobParam = XxlJobHelper.getJobParam();
        if (!StrUtil.isEmpty(jobParam) && JSONUtil.isTypeJSON(jobParam)) {
            try {
                condition = JSONUtil.toBean(jobParam, QueryCondition.class);
            } catch (Exception e) {
                log.warn("参数无效：{}", jobParam);
            }
        }

        if (condition == null) {
            condition = new QueryCondition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            Calendar start = Calendar.getInstance();
            start.add(Calendar.MONTH, -3);
            condition.setBeginTime(dateFormat.format(start.getTime()));
            condition.setEndTime(dateFormat.format(now.getTime()));
        }

        int flagUpdateGoodsInfo = dataCatchService.updateBLHGoodsDatas(condition);
        return new JSONArray();
    }

    /**
     * 百礼汇更新图片数据的任务
     *
     * @param param
     * @throws Exception
     */
    @XxlJob("DataCatch-bai-li-hui-update-img")
    public JSONArray updateGoodsImagesJobForBLH(String param) throws Exception {
        QueryCondition condition = null;
        String jobParam = XxlJobHelper.getJobParam();
        if (!StrUtil.isEmpty(jobParam)) {
            try {
                condition = JSONUtil.toBean(jobParam, QueryCondition.class);
            } catch (Exception e) {
                log.warn("参数无效：{}", jobParam);
            }
        }
        if (condition == null) {
            condition = new QueryCondition();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar now = Calendar.getInstance();
            Calendar start = Calendar.getInstance();
            start.add(Calendar.MONTH, -3);
            condition.setBeginTime(dateFormat.format(start.getTime()));
            condition.setEndTime(dateFormat.format(now.getTime()));
        }

        int flagUpdateGoodsInfo = dataCatchService.updateBLHImagesDatas(condition);
        return new JSONArray();
    }
}
